
#include  <msp430x54xA.h>
#include  "Drivers.h"
#include  "..\ESS_CommonDefine.h"


volatile unsigned int A0results[Num_of_Results];
volatile unsigned int A1results[Num_of_Results];
volatile unsigned int A2results[Num_of_Results];
//volatile unsigned int A3results[Num_of_Results];
//volatile unsigned int A4results[Num_of_Results];
//volatile unsigned int A5results[Num_of_Results];
//volatile unsigned int A6results[Num_of_Results];
//volatile unsigned int A7results[Num_of_Results];

static unsigned int A0results_Avg;
static unsigned int A1results_Avg;
static unsigned int A2results_Avg;
//static unsigned int A3results_Avg;
//static unsigned int A4results_Avg;
//static unsigned int A5results_Avg;
//static unsigned int A6results_Avg;
//static unsigned int A7results_Avg;

static unsigned int index;

//float divided = 2.5f/4095;

////////////////////////////////////////////////////////////////////////////
// Adc Reader configuration
// channel A0 : OPA DSG Current sense (+)
// channel A1 : OPA DSG Current sense (+)
// channel A2 : ADG8 for System Current sense (+/-)
// channel A3 : reserved or Temperature 1
// channel A4 : reserved or Temperature 2
// channel A5 : reserved or Temperature 3
// channel A6 : reserved or Temperature 4
// channel A7 : reserved or Temperature 5
////////////////////////////////////////////////////////////////////////////
void InitAdcReader(){
  // Set up pins used by function unit
  ADC_PORT_SEL = 0xFF;
  /////////////////////////////////////////////////
  
  /* Initialize REF module */
  // Enable 2.5V shared reference, disable temperature sensor to save power
  REFCTL0 |= REFMSTR+REFVSEL_2+REFON+REFTCOFF; //FOR f5438A
  ADC12CTL0 = ADC12ON+ADC12MSC+ADC12SHT0_5+ADC12REFON+ADC12REF2_5V;

  /* Initialize ADC12 */  
  //ADC12MCTL0 = ADC12SREF_1;                 // Vr+=Vref+ and Vr-=AVss
  ADC12CTL1 = ADC12SHP+ADC12CONSEQ_3;       // Use sampling timer, repeated sequence
  ADC12MCTL0 = ADC12INCH_0+ADC12SREF_1;                 // ref+=AVcc, channel = A0
  ADC12MCTL1 = ADC12INCH_1+ADC12SREF_1;                 // ref+=AVcc, channel = A1
  ADC12MCTL2 = ADC12INCH_2+ADC12SREF_1+ADC12EOS;;                 // ref+=AVcc, channel = A2 , end seq.
//  ADC12MCTL3 = ADC12INCH_3+ADC12SREF_1;                 // ref+=AVcc, channel = A3
//  ADC12MCTL4 = ADC12INCH_4+ADC12SREF_1;                 // ref+=AVcc, channel = A4
//  ADC12MCTL5 = ADC12INCH_5+ADC12SREF_1;                 // ref+=AVcc, channel = A5
//  ADC12MCTL6 = ADC12INCH_6+ADC12SREF_1;                 // ref+=AVcc, channel = A6
//  ADC12MCTL7 = ADC12INCH_7+ADC12SREF_1+ADC12EOS;        // ref+=AVcc, channel = A7, end seq.
//  ADC12IE = ADC12IE7;                           // Enable ADC12IFG.7


  ADC12IE = ADC12IE2;                           // Enable ADC12IFG.2
  G_Device_Action_Controls &= ~ADC_CONVERSION;
  index = 0;
  
}
//about 400us ~ 510us ->start to stop
void startAdcConversion(){
  index = 0;
  ADC12CTL0 |= ADC12ENC;                    // Enable conversions
  ADC12CTL0 |= ADC12SC;                     // Start convn - software trigger
  //__delay_cycles(105200);                   // for fill-in all arrays
  G_Device_Action_Controls |= ADC_CONVERSION;
}
void stopAdcConversion(){
  ADC12CTL0 &= ~ADC12SC;                     // Start convn - software trigger
  ADC12CTL0 &= ~ADC12ENC;                    // Disable conversions
  index = 0;
  G_Device_Action_Controls &= ~ADC_CONVERSION;
}

//OPA
unsigned int getDSGCurrentADC(){
  int i = 0;
  A0results_Avg = 0;
  for(i=0; i<Num_of_Results; i++){
    A0results_Avg += A0results[i];
  }
  A0results_Avg = A0results_Avg >> 2;
  return A0results_Avg;
  //return 0;
}
//OPA
unsigned int getCHGCurrentADC(){
  int i = 0;
  A1results_Avg = 0;
  for(i=0; i<Num_of_Results; i++){
    A1results_Avg += A1results[i];
  }
  A1results_Avg = A1results_Avg >> 2;
  return A1results_Avg;
  //return 0;
}

//ADG80
unsigned int getSystemCurrentADC(){
  int i = 0;
  A2results_Avg = 0;
  for(i=0; i<Num_of_Results; i++){
    A2results_Avg += A2results[i];
  }
  A2results_Avg = A2results_Avg >> 2;
  return A2results_Avg;
}
//unsigned int getESSModuleVoltage(){
//  int i = 0;
//  A3results_Avg = 0;
//  for(i=0; i<Num_of_Results; i++){
//    A3results_Avg += A3results[i];
//  }
//  A3results_Avg = A3results_Avg >> 3;
//  return A3results_Avg;
//}
//
//
//unsigned int getTemp1ADC(){
//  int i = 0;
//  A4results_Avg = 0;
//  for(i=0; i<Num_of_Results; i++){
//    A4results_Avg += A4results[i];
//  }
//  A4results_Avg = A4results_Avg >> 3;
//  return A4results_Avg;
//}
//unsigned int getTemp2ADC(){
//  int i = 0;
//  A5results_Avg = 0;
//  for(i=0; i<Num_of_Results; i++){
//    A5results_Avg += A5results[i];
//  }
//  A5results_Avg = A5results_Avg >> 3;
//  return A5results_Avg;
//}
//unsigned int getTemp3ADC(){
//  int i = 0;
//  A6results_Avg = 0;
//  for(i=0; i<Num_of_Results; i++){
//    A6results_Avg += A6results[i];
//  }
//  A6results_Avg = A6results_Avg >> 3;
//   return A6results_Avg;
//}
//unsigned int getTemp4ADC(){
//  int i = 0;
//  A7results_Avg = 0;
//  for(i=0; i<Num_of_Results; i++){
//    A7results_Avg += A7results[i];
//  }
//  A7results_Avg = A7results_Avg >> 3;
//  return A7results_Avg;
//}


#pragma vector=ADC12_VECTOR
__interrupt void ADC12ISR (void)
{
  //static unsigned int index = 0;
  //static int i = 0 ;
  
  switch(__even_in_range(ADC12IV,34))
  {
  case  0: break;                           // Vector  0:  No interrupt
  case  2: break;                           // Vector  2:  ADC overflow
  case  4: break;                           // Vector  4:  ADC timing overflow
  case  6: break;                           // Vector  6:  ADC12IFG0
  case  8: break;                           // Vector  8:  ADC12IFG1
  case 10:                                  // Vector 10:  ADC12IFG2
    A0results[index] = ADC12MEM0 & 0x0FFF;           // Move A0 results, IFG is cleared
    A1results[index] = ADC12MEM1 & 0x0FFF;           // Move A1 results, IFG is cleared
    A2results[index] = ADC12MEM2 & 0x0FFF;           // Move A2 results, IFG is cleared
    index++;                                // Increment results index, modulo; Set Breakpoint1 here
    if (index >= Num_of_Results)
    {
      stopAdcConversion();     
      index = 0;
    }//if (index >= Num_of_Results)
    //ADC12IFG = 0;                           // Clear the interrupt flags
    
    break;
  case 12: break;                           // Vector 12:  ADC12IFG3
  case 14: break;                           // Vector 14:  ADC12IFG4
  case 16: break;                           // Vector 16:  ADC12IFG5
  case 18: break;                           // Vector 18:  ADC12IFG6
  case 20:                                  // Vector 20:  ADC12IFG7

    break;
  case 22: break;                           // Vector 22:  ADC12IFG8
  case 24: break;                           // Vector 24:  ADC12IFG9
  case 26: break;                           // Vector 26:  ADC12IFG10
  case 28: break;                           // Vector 28:  ADC12IFG11
  case 30: break;                           // Vector 30:  ADC12IFG12
  case 32: break;                           // Vector 32:  ADC12IFG13
  case 34: break;                           // Vector 34:  ADC12IFG14
  default: break; 
  }
}

